package com.glory.brepda.activity.technicalservice;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.ToolBindActivity;
import com.glory.brepda.adapter.ReplaceEqpListAdapter;
import com.glory.brepda.adapter.ReplaceToolListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.RepairFromEntity;
import com.glory.brepda.entity.RepairFromListEntity;
import com.glory.brepda.entity.RepairSheetDataEntity;
import com.glory.brepda.entity.ToolEntity;
import com.glory.brepda.entity.ToolListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ReplaceToolActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvEqp)
    TextView tvEqp;
    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvReplaceList)
    RecyclerView rvReplaceList;
    @BindView(R.id.tvOwner)
    TextView tvOwner;
    @BindView(R.id.tvClazz)
    TextView tvClazz;
    @BindView(R.id.tvAddTool)
    TextView tvAddTool;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.tvStartRepair)
    TextView tvStartRepair;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.llBtnLayout)
    LinearLayout llBtnLayout;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;
    private ReplaceEqpListAdapter mReplaceEqpListAdapter;
    private RepairFromListEntity.RepairData mData;
    private ReplaceToolListAdapter mAdapter;
    private String mFrom = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_replace_material;
    }

    @Override
    protected void initView() {
        tvBaseSubmit.setVisibility(View.GONE);
        tvTitle.setText("换Tooling");
        setMainLayout(llMain);
        listTv1.setText("类型");
        listTv2.setText("编号");
        listTv3.setText("操作");
        listTv4.setVisibility(View.GONE);
        initRecyclerView(rvReplaceList, this);
        mData = (RepairFromListEntity.RepairData) getIntent().getExtras().getSerializable("REPAIRFROM");
        mFrom = getIntent().getExtras().getString("FROM");
        tvEqp.setText(mData.getREPAIRSHEETDATA().getEQUIPMENTID());
        tvLocation.setText(mData.getREPAIRSHEETDATA().getLOCATION());
        tvLot.setText(mData.getREPAIRSHEETDATA().getLOTID());
        tvClazz.setText(mData.getREPAIRSHEETDATA().getSHIFT());
        tvOwner.setText(mData.getREPAIRSHEETDATA().getOWNER());
        etComment.setText(mData.getREPAIRSHEETDATA().getREPAIRCOMMENT());
        tvStartRepair.setText(mData.getREPAIRFORM().getSTATUS().equals("START") ? "开始调机" : "调机完成");
        mAdapter = new ReplaceToolListAdapter(null);
        mAdapter.isFirstOnly(true);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvReplaceList.setAdapter(mAdapter);
        mAdapter.setStart(mData.getREPAIRFORM().getSTATUS().equals("START"));
        tvAddTool.setVisibility(mData.getREPAIRFORM().getSTATUS().equals("START") ? View.GONE : View.VISIBLE);
        mAdapter.setEqp(tvEqp.getText().toString());
        if (mFrom != null && mFrom.equals("QC")) {
            updateUI();
        }
        initRecyclerView(rvEqpList, this);
        mReplaceEqpListAdapter = new ReplaceEqpListAdapter(null);
        mReplaceEqpListAdapter.isFirstOnly(true);
        mReplaceEqpListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rvEqpList.setAdapter(mReplaceEqpListAdapter);
        mReplaceEqpListAdapter.setNewData(mData.getEQPMATERIALS());
    }

    private void updateUI() {
        tvBaseSubmit.setVisibility(View.GONE);
        llBtnLayout.setVisibility(View.GONE);
        etComment.setEnabled(false);
        tvAddTool.setVisibility(View.GONE);
        mAdapter.setStart(true);
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseResponseBean<ToolListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ToolListEntity>>
                (new TypeToken<BaseResponseBean<ToolListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ToolListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<ToolEntity> toollist = response.body().getResponse().getBody().getTOOLLIST();
                    mAdapter.setNewData(toollist);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ToolListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TOOLREPORT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","LOAD");
        map.addProperty("EQUIPMENTID",tvEqp.getText().toString());
//        NetUtils.requestPostNet("CPC.TOOLREPORT","TOOLREPORT",this,map,callBack);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    @OnClick({R.id.tvStartRepair,R.id.tvAddTool})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tvAddTool){
            FreeApi_Utils.skipWithExtraIntent("header",this, ToolBindActivity.class);
        }else if (view.getId() == R.id.tvStartRepair){
            showSubmitDialog(new onSubmitListener() {
                @Override
                public void setOnSubmitListener() {
                    FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                            (new TypeToken<BaseResponseBean<JSONObject>>() {
                            }.getType(), getSupportFragmentManager(), this) {

                        @Override
                        public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                            super.onSuccess(response);
                            if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                if (tvStartRepair.getText().toString().equals("开始调机")){
                                    showToast("开始调机成功",true);
                                    tvStartRepair.setText("调机完成");
                                    mAdapter.setStart(false);
                                    tvAddTool.setVisibility(View.VISIBLE);
                                    mData.getREPAIRFORM().setSTATUS("STARTFORM");
                                    mData.getREPAIRSHEETDATA().setSTARTREPAIRTIME(FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"));
                                    mData.getREPAIRSHEETDATA().setREPAIROWNER(Constant.CURRENT_USER);
                                }else {
                                    showConfirmDialog(ReplaceToolActivity.this,"提交成功");
                                }

                            }
                        }

                        @Override
                        public void onError(Response<BaseResponseBean<JSONObject>> response) {
                            super.onError(response);
                            loadError(response.getException(),"PMSREPAIR");

                        }
                    };

                    JsonObject map = new JsonObject();
                    map.addProperty("ACTIONTYPE",tvStartRepair.getText().toString().equals("开始调机") ? "STARTFORM" : "EXECUTE");
                    map.addProperty("REPAIRTYPE","changeTool");
                    //2023.11.21技质部三次检验没改好，一分厂先跑起来，把QC部分剔除
                    map.addProperty("QC",rbYes.isChecked()?"Y":"N");
                    mData.getREPAIRFORM().setOWNER(Constant.CURRENT_USER);
                    RepairFromEntity repairform = mData.getREPAIRFORM();
                    map.add("REPAIRFORM",new JsonParser().parse(new Gson().toJson(repairform)).getAsJsonObject());
                    RepairSheetDataEntity repairsheetdata = mData.getREPAIRSHEETDATA();
                    if (tvStartRepair.getText().toString().equals("调机完成")){
                        repairsheetdata.setREPAIRCOMMENT(getText(etComment));
                    }
                    map.add("REPAIRSHEETDATA",new JsonParser().parse(new Gson().toJson(repairsheetdata)).getAsJsonObject());
                    NetUtils.requestPostNet("IF.PMSREPAIR","PMSREPAIR",ReplaceToolActivity.this,map,callBack);
                }
            });

        }
    }

}
